// pages/orderCenter/detailv2.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderCenter: {

    }

  },
  //查询委托单数据
  getById() {
    const formData = {
      openId: this.data.openId,
      criteria: {
        orderCenter: this.data.truckCenterId ? {} : {
          id: this.data.orderCenterId,
        },
        truckCenterId: this.data.truckCenterId,
        publishSide: this.data.publishSide,
      }
    }
    requestUtil.postRequest(requestUrls.truckOrder.getById, formData, (res) => {
      console.log(res);
      if (!res.hasError) {
        const { orderCenter = {}, transportConfirmForm = {} } = res.result;
        const { orderTruckImageList = [], truckCenterList = [] } = orderCenter;
        if (orderTruckImageList && orderTruckImageList.length > 0) {
          let imageList = [];
          orderTruckImageList.forEach(image => {
            imageList.push({
              ...image,
              imageUrl: requestUrls.baseImagePrefixUrl + image.imageUrl,
            })
          })
          this.setData({
            orderCenterImageList: imageList,
            goodsImages: imageList,
          })
        }
        if (truckCenterList && truckCenterList.length > 0) {
          let truckList = [];
          truckCenterList.forEach(item => {
            truckList.push({
              ...item,
              freeBeginTime: commonUtils.formatDate(item.freeBeginTime, 'yyyy-MM-dd'),
              freeEndTime: commonUtils.formatDate(item.freeEndTime, 'yyyy-MM-dd'),
            })
          })
          this.setData({
            truckCenterList: truckList
          });
          if (this.data.publishSide === 'TRUCK_CENTER') {
            this.setData({
              truckCenterId: truckList[0].id
            });
          }
        }
        if (transportConfirmForm) {
          this.setData({
            transportConfirmForm: {
              ...transportConfirmForm,
              statusName: transportConfirmForm.orderId ? (transportConfirmForm.step === '2' ? '已装货' : '已卸货') : '待装货',
              packageDate: commonUtils.formatDate(transportConfirmForm.packageDate, 'yyyy-MM-dd'),
              receiverDate: commonUtils.formatDate(transportConfirmForm.receiverDate, 'yyyy-MM-dd'),
              createdTime: commonUtils.formatDate(transportConfirmForm.createdTime, 'yyyy-MM-dd'),
            }
          })
        }

        this.setData({
          orderCenter: {
            ...orderCenter,
            loadingBeginTime: commonUtils.formatDate(orderCenter.loadingBeginTime, 'yyyy-MM-dd'),
            loadingEndTime: commonUtils.formatDate(orderCenter.loadingEndTime, 'yyyy-MM-dd'),
            statusName: this.data.publishSide === 'TRUCK_CENTER' && truckCenterList && truckCenterList.length > 0 ? '申请中' : orderCenter.statusName,
          },
          //如果进入角色为货主，并且委托单信息不为空，则显示取消按钮；否则显示申请按钮
          showBtn: this.data.publishSide === 'TRUCK_CENTER' && this.data.truckCenterId ? 'cancel' : 'apply',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { orderCenterId = '', truckCenterId = '', publishSide = '' } = options;
    this.setData({
      orderCenterId: orderCenterId,
      truckCenterId: truckCenterId,
      publishSide: publishSide
    });
    this.getById();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})